#!/usr/bin/python3

import pymysql
from PyQt5.QtCore import QObject, pyqtSignal


def db_close(f):
    def method(*args, **kw):
        self = args[0]
        f(self)
        self.db.close()
        return None

    return method


class Mydb(QObject):
    completed = pyqtSignal(list)  # 第一步：定义信号

    def __init__(self, parent=None):
        super(Mydb, self).__init__(parent)
        # 打开数据库连接
        self.db = pymysql.connect(host='localhost',
                                  user='root',
                                  password='1234',
                                  database='students')
        self.cursor = self.db.cursor()

    @db_close
    def query(self):
        stu_list = []
        self.cursor.execute("SELECT * from stu_data")
        # 使用 fetchone() 方法获取单条数据.
        data = self.cursor.fetchall()
        for i in data:
            name = i[1]
            sex = i[2]
            age = i[3]
            score = i[4]
            stu_list.append([name, sex, age, score])

        self.completed.emit(stu_list)  # 发送


def func():
    list = [100, 200]
    return list
